package com.chixing.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {

    public static final String PUBLIC_KEY_BASE64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAquQUeXu0+PLZ059RmWVv6xhy3r99+kDX\n" +
            "R2xK70JjfesCb2O7W82ikYiLMJPtGoo9aHaDM4Vf9A0ANg3PzpjeTDuXPiGAdqv254bk3VkfBgZO\n" +
            "R1F1eFbVQHw2VmIH56wmGnhysPpq4hFMu9H6MiG8ztMY7vnpA5xV8kJ9sLOJBVv0pGN5oJt4aKW3\n" +
            "9Ofr7tE+j94VIwI9NYP9M9tR2vRHMteinuYEdcyOauYpc0AvbAvkKGt0yN5ae9OKzAmiS9uGVuaB\n" +
            "73aqBmZlcBsZl8BGh6oA+KSupXlxxgD/7/vcyotBDaGS747LIqwPVgAswHqJ7KY7PMdHYYdHfGZs\n" +
            "EErMDwIDAQAB";

    public static final String PRIVATE_KEY_BASE64 = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCq5BR5e7T48tnTn1GZZW/rGHLe\n" +
            "v336QNdHbErvQmN96wJvY7tbzaKRiIswk+0aij1odoMzhV/0DQA2Dc/OmN5MO5c+IYB2q/bnhuTd\n" +
            "WR8GBk5HUXV4VtVAfDZWYgfnrCYaeHKw+mriEUy70foyIbzO0xju+ekDnFXyQn2ws4kFW/SkY3mg\n" +
            "m3hopbf05+vu0T6P3hUjAj01g/0z21Ha9Ecy16Ke5gR1zI5q5ilzQC9sC+Qoa3TI3lp704rMCaJL\n" +
            "24ZW5oHvdqoGZmVwGxmXwEaHqgD4pK6leXHGAP/v+9zKi0ENoZLvjssirA9WACzAeonspjs8x0dh\n" +
            "h0d8ZmwQSswPAgMBAAECggEAF7/i6I88s2xswXBe0sa6Kx4WuMY8J89jG9X9BAth8Fn82KYvn66Q\n" +
            "bVFgpruoNAwKRL5TtZFPqIc2BE0fGEM+gbG2bzgQtm1nvv2MSQbdvOSbdGXpwXOv52pb0t2E47mV\n" +
            "AhDDP71NpWVqGKAx+U2fA89qo6uvqoTLnQOInEoXPNs0kMdUdJkEYiklFwzmFvtbNwa9O/1ogWMX\n" +
            "+GuGBQD4Uxo0TDgl20iXKa0udVkCJttLkdGHKdGEq6hLKz5Hi7PJSH4hZLo//nuh52r6WK4G2ZYl\n" +
            "/FXeJIUvRBmUVva66arVglVYLcd3vo1ygkCGDfLIqUQ+TgzNmgwnrKRfczwbAQKBgQDaCS0Capu9\n" +
            "kLdosxCVwzkXBhQe7pjJkW/WnzsvO8TdCzKHhZBFwKZX86j7lnQl2fyu35T0kNXdruTOoJtzT8Ji\n" +
            "58iCCGQVN4kJW8C7h5eRqQ5cL1/49lyJJOUfIroSCktrbzJySOZUbQonYd+qdwdUiS/+FEov8nNg\n" +
            "NqVEvstbIQKBgQDIpXJa/k4tbm4MsabZCmMjiUjGHvnomb8d0yZrPGOLUbwO43gW+uYPRuKO0Ttb\n" +
            "Fcj8BuyCX5bAj9tRYB5kq4FxyfnbZTo/kQoYbJWuHzbF+Dax3tvbR89WxfYzwkPgGesWHr9Uuue3\n" +
            "3H/Pc9+EPbvgZBxro5+BJiyzpTSAtcvxLwKBgQCGXglC5/g6hqjA+TlLUhgovDJCLcAy2FIX7eFH\n" +
            "ZooXvwLQydrzYZohsaN+CGZjvUcfbeIaJffmuhV/0TJ7ibe9xCGpIQqsmKD3B3hrGVCrD4gt/3Qh\n" +
            "tZGpYvdgjMImw/leqBRqocpwVRJg2sw//UKjMjhFWLOvgLbhoEuRZhNkgQKBgQCBWGaH0jiDgRTg\n" +
            "igWNFO0kE3QehAmGsNZLW7sAlkV1opMg5fKcndGBN6kFeExTQ4/1KGF/CjAcoqA3rUbqjsU7nYC8\n" +
            "tlz+bXmclSNIxEaDCKf5sjj4+ynmPmegpjMrXIn4EIo3DOYj2Llr+r60eTeMR/0qRqikkP9wL6fQ\n" +
            "8WEvcQKBgQCkSZJ7bEwCNZnsejcBPFBncPCvRrrkjqyR5Oz6k4U6AvfgydgEQ+IXl9VN6Jj4HSE1\n" +
            "Jg9RIxsniAjXhSc9HhFoUzK+X5OImcSVEdOVrChdWhjsOQyAXVNIeVs/oCr2b6ZziEXpba8U8QEY\n" +
            "SjlGJmhBzhqbDJSQGalS295oqzqOPQ==";

    //生成秘钥对
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    //获取公钥(Base64编码)
    public static String getPublicKey(KeyPair keyPair) {
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return byte2Base64(bytes);
    }

    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair) {
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return byte2Base64(bytes);
    }

    //将Base64编码后的公钥转换成PublicKey对象
    public static PublicKey string2PublicKey(String pubStr) throws Exception {
        byte[] keyBytes = base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    //将Base64编码后的私钥转换成PrivateKey对象
    public static PrivateKey string2PrivateKey(String priStr) throws Exception {
        byte[] keyBytes = base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }


}
